import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthy_station_flutter/utils/res_colors.dart';
import 'package:provider/provider.dart';

import '../nav/nav_route.dart';
import '../provider/provider_app.dart';
import '../utils/log_utils.dart';
import '../utils/res_images.dart';
import '../utils/screen_adapter.dart';

/// 
/// @Description
/// @Author WuJianFeng
/// @Date 2024/3/1 11:01
/// 

class TitleBarWidget extends StatefulWidget{
  const TitleBarWidget({super.key});
  @override
  State<StatefulWidget> createState() => _TitleBarWidgetState();
  
}

class _TitleBarWidgetState extends State<TitleBarWidget>{
  late EventChannel _eventChannel;
  @override
  void initState() {
    super.initState();
    _eventChannel = const EventChannel("time_channel");
    _eventChannel.receiveBroadcastStream().listen( (event){
      LogUtils.d("Flutter:Flutter receive from Android:$event");
      AppInfo().setCurrentTime("$event");
    },onDone: (){
      LogUtils.d("完成");
    },onError: (error){
      LogUtils.d("失败：$error");
    });

  }
  @override
  Widget build(BuildContext context) {
    return Container(
        decoration:  ModalRoute.of(context)?.settings.name != routerHomePage? const BoxDecoration(
          gradient:  LinearGradient(
            begin: FractionalOffset.topCenter,
            end: FractionalOffset.bottomCenter,
            colors: [
              color_c8ebff,
              color_dbf0fd,
            ],
          ),
        ) : null,

      height: Adapt.pt(133),
      padding: EdgeInsets.only(left: Adapt.pt(40), right: Adapt.pt(40)),
      child: Row(
        children: [
          /// 图标
          SizedBox(
            // alignment: Alignment.centerLeft,
            width: Adapt.pt(69),
            // height: Adapt.pt(61),
            child: ImageUtils.getImg("app_icon.webp"),
          ),

          /// 站点名
          Expanded(
              child: Container(
                padding: EdgeInsets.only(left: Adapt.pt(14)),
                alignment: Alignment.centerLeft,
                child: Consumer<AppInfo>(
                  builder: (context, AppInfo appInfo, child) {
                    return Text(
                      appInfo.stationName,
                      style: TextStyle(
                        fontSize: Adapt.pt(45),
                        color: color_203146,
                        fontWeight: FontWeight.bold,
                      ),
                    );
                  },
                ),
              )
          ),

          /// 时间
          Container(
            margin: EdgeInsets.only(right: Adapt.pt(49)),
            alignment: Alignment.center,
            child: Consumer<AppInfo>(
              builder: (context, AppInfo appInfo, child) {
                return Text(
                  appInfo.currentTime,
                  style: TextStyle(
                    fontSize: Adapt.pt(30),
                    color: ModalRoute.of(context)?.settings.name != routerHomePage?color_434343 : Colors.white,
                  ),
                )
                  ;
              },
            ),
          ),

          /// 设备id
          Container(
            alignment: Alignment.center,
            child: Consumer<AppInfo>(
              builder: (context, AppInfo appInfo, child) {
                return Text(
                  "ID: ${appInfo.machineId}",
                  style: TextStyle(
                    fontSize: Adapt.pt(30),
                    color: ModalRoute.of(context)?.settings.name != routerHomePage?color_434343 : Colors.white,
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
  
}
 
 